package com.jstyle.jclife.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.jclife.R;
import com.jstyle.jclife.model.JstyleDevice;



import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/3/28.
 */

public class DeviceListAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    List<JstyleDevice> devices;


    public DeviceListAdapter(List<JstyleDevice> deviceList, onItemClickListener onItemClickListener) {
        this.devices = deviceList;
        this.itemClickListener=onItemClickListener;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_devicetype, parent, false);
        return new DeviceListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        DeviceListViewHolder deviceListViewHolder=(DeviceListViewHolder)holder;
        final JstyleDevice jstyleDevice=devices.get(position);
        ((DeviceListViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(itemClickListener!=null)itemClickListener.onItemClick(jstyleDevice);
            }
        });
        deviceListViewHolder.ivDevice.setImageResource(jstyleDevice.getDeviceSelectedDrawableId());
        deviceListViewHolder.tvContent.setText(jstyleDevice.getDeviceDescriptorId());
        deviceListViewHolder.tvName.setText(jstyleDevice.getDeviceNameId());
    }

    @Override
    public int getItemCount() {
        return devices.size();
    }

    public static class DeviceListViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_device_name)
        TextView tvName;
        @BindView(R.id.tv_device_content)
        TextView tvContent;
        @BindView(R.id.iv_device)
        ImageView ivDevice;
        public DeviceListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    onItemClickListener itemClickListener;
    public interface onItemClickListener{
        public void onItemClick(JstyleDevice jstyleDevice);
    }
}
